<?php
/**
 *
 *  
 * Date: 2024/1/16
 * Email: < >
 */


namespace app\traits;



trait CommonScript
{
    function http_post($url,$data){
        // 初始化一个cURL句柄
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_POST, true);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
        curl_setopt($ch,CURLOPT_TIMEOUT,30);
        curl_setopt($ch,CURLOPT_HEADER,false);
        curl_setopt($ch,CURLOPT_RETURNTRANSFER,true);
        // 执行cURL请求
        $result = curl_exec($ch);
        if(curl_errno($ch)){
            return 'Error POST'.curl_error($ch);
        }
        curl_close($ch);
        return $result;
    }

    function base_pay($req)
    {
        $apiUrl = getenv('PAY_URL');
        $merchantKey = getenv('PAY_SECRET');
        $data = array(
            'merchantId' => getenv('PAY_ID'),
            'type' => $req['type'],
            'orderId' => $req['uid'].time(),
            'amount' => $req['price'],
            'notifyUrl' => 'http://154.23.177.10:8080/public/pay/notify',
            'returnUrl' => 'http://154.23.177.10:8080/public/pay/success.html',
            'failUrl' => 'http://154.23.177.10:8080/public/pay/fail.html',
            'ip' => $req['ip'],
            'attach' => '8'
        );
        ksort($data);
        $content = '';
        foreach ($data as $key => $val) {
            $content .= $key . '=' . $val . '&';
        }
        $content .= 'key='. $merchantKey;
        $sign = md5($content);
        $data['sign'] = $sign;
        $res = $this->http_post($apiUrl,$data);
        return $res;
    }
    function pay_query($req)
    {
        $apiUrl = getenv('PAY_QUERY');
        $merchantKey = getenv('PAY_SECRET');
        $data = array(
            'merchantId' => getenv('PAY_ID'),
            'orderId' => $req['orderId']
        );
        ksort($data);
        $content = '';
        foreach ($data as $key => $val) {
            $content .= $key . '=' . $val . '&';
        }
        $content .= 'key='. $merchantKey;
        $sign = md5($content);
        $data['sign'] = $sign;
        $res = $this->http_post($apiUrl,$data);
        return $res;
    }
    function pay_query_balance()
    {
        $apiUrl = getenv('PAY_QUERY_BALANCE');
        $merchantKey = getenv('PAY_SECRET');
        $data = array(
            'merchantId' => getenv('PAY_ID'),
        );
        ksort($data);
        $content = '';
        foreach ($data as $key => $val) {
            $content .= $key . '=' . $val . '&';
        }
        $content .= 'key='. $merchantKey;
        $sign = md5($content);
        $data['sign'] = $sign;
        $res = $this->http_post($apiUrl,$data);
        return $res;
    }


    function truncateTo5DecimalPlaces($number) {
        $decimal_point_position = strpos($number, ".");
        if ($decimal_point_position === false) { // 如果没有小数点，直接返回原数
            return $number;
        }

        // 计算小数部分长度
        $decimal_length = strlen(substr($number, $decimal_point_position + 1));

        // 如果小数部分不足5位，直接返回
        if ($decimal_length <= 5) {
            return $number;
        }

        // 截取到小数点后5位
        $truncated_number = substr($number, 0, $decimal_point_position + 5);

        // 使用str_pad()填充0以保持总位数
        return str_pad($truncated_number, strlen($number), '0', STR_PAD_LEFT);
    }

    function generateRandomCode($length = 6) {
        // 生成一个0到999999的随机整数
        $random_int = rand(0, 999999);

        // 截取6位数字
        $code = substr($random_int, 0, $length);

        return $code;
    }

}